package ruan_kao;

abstract class CState {
    public int flyMiles;

    public abstract double travel(int miles, FrequentFlyer context); //1
}

class CNoCustomer extends CState {
    @Override
    public double travel(int miles, FrequentFlyer context) {
        System.out.println("Your travel will not account for points");
        return miles;
    }
}

class CBasic extends CState {
    @Override
    public double travel(int miles, FrequentFlyer context) {
        if (context.flyMiles >= 25000 && context.flyMiles < 50000) {
            context.setState(new CSilver()); //2
        }
        if (context.flyMiles >= 50000) {
            context.setState(new CGold()); //3
        }
        return miles;
    }
}

class CGold extends CState {
    @Override
    public double travel(int miles, FrequentFlyer context) {
        if (context.flyMiles >= 25000 && context.flyMiles < 50000) {
            context.setState(new CSilver()); //4
        }
        if (context.flyMiles < 25000) {
            context.setState(new CBasic()); //5
        }
        return miles + 0.5 * miles;
    }
}

class CSilver extends CState {
    @Override
    public double travel(int miles, FrequentFlyer context) {
        if (context.flyMiles <= 25000) {
            context.setState(new CBasic());
        }
        if (context.flyMiles >= 50000) {
            context.setState(new CGold());
        }
        return miles + 0.25 * miles;
    }
}

class FrequentFlyer {
    CState state;
    double flyMiles;

    public FrequentFlyer() {
        state = new CNoCustomer();
        flyMiles = 0;
        setState(state);
    }

    public void setState(CState state) {
        this.state = state;
    }

    public void travel(int miles) {
        double bonusMiles = state.travel(miles, this);
        flyMiles = flyMiles + bonusMiles;
    }
}
